package br.com.portalnet.persistence;


import br.com.portalnet.model.Nivel;
import br.com.portalnet.util.HibernateUtil;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;



public class NivelDAO implements INivelDAO {

	@Override
	public void save(Nivel nivel) {
		Session session = null;
		try
		{
			session= HibernateUtil.getSession().openSession();
		
			Transaction t = session.beginTransaction();
			session.save(nivel);
			t.commit();
			
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			
		}
		
		finally 
		{
			session.close();
			
		
		}
	}

	@Override
	public Nivel getNivel(long id) {
		
		Session session = null;
		try
		{
			session = HibernateUtil.getSession().openSession();		
			return (Nivel) session.get(Nivel.class,id);
		
		}finally 
		{
			
			try
			{
				
				
				if(session != null)
				{
					session.close();
				}
			
				
			} catch (Exception e) {
				
			}
		
		}
	}

	@Override
	public List<Nivel> list() {
		Session session = null;
		
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			//List lista = session.createQuery("from Livro").list();		
			List lista = session.createCriteria(Nivel.class).list();
			t.commit();
			return lista;
			
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			
			return null;
		}
		
		finally 
		{
			session.close();
			
		
		}
	}

	@Override
	public void remove(Nivel nivel) {
		Session session = null;
		
		try
		{
			
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			session.delete(nivel);
			t.commit();
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			
		}
		
		finally 
		{
			session.close();
			
		
		}
			
	}

	@Override
	public void update(Nivel nivel) {
		
		Session session = null;
		
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			session.update(nivel);
			t.commit();
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			
		}
		
		finally 
		{
			session.close();
			
		
		}
			
	}

	


}
